<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Mmod_home extends MYAD_Model {

    protected $table_name = 'm_user';
    protected $table_company = 'm_company';

    function __construct() {
        parent::__construct();
        $this->load->model('mod_user/Mmod_user', 'User');
        //$this->load->library('email');
    }

    /**
     * @author Thanh NV        
     * @created:2014/06/23
     * @function changePassword              
     * @Output: TRUE or FALSE
     * @This function change password for user
     */
    function changePassword() {
        $this->load->library('form_validation');
        $sub = ' ';
        if ($this->input->post('btnSubmit')) {
            $this->form_validation->set_rules('txtCurrentPassword', $this->lang->line('Home_CurrentPasword'), 'required|min_length[8]|max_length[32]|callback_check_space|xss_clean');
            $this->form_validation->set_rules('txtNewPassword', $this->lang->line('Home_NewPasword'), 'required|min_length[8]|max_length[32]|callback_check_space|xss_clean');
            $this->form_validation->set_rules('txtConfirmPassword', $this->lang->line('Home_ConfimNewPasword'), 'trim|required|min_length[8]|max_length[32]|xss_clean');
            if ($this->form_validation->run() == TRUE) {
                $username = $this->session->userdata('UserName');
                $password = $this->input->post('txtCurrentPassword');
                $data = $this->User->getUserLogin($username, md5($password),$this->session->userdata('CompanyCode'));
                if (sizeof($data) > 0) {
                    if (strlen(strstr($this->input->post('txtNewPassword'), $sub)) > 0) {
                        $temp['CurrentPassword'] = $this->input->post('txtCurrentPassword');
                        $temp['message'] = $this->lang->line('Home_Space');
                        $temp['flag'] = FALSE;
                    } elseif (strcmp(trim($this->input->post('txtNewPassword')), trim($this->input->post('txtConfirmPassword'))) !== 0) {
                        $temp['message'] = $this->lang->line('Home_NewConfirmPasword');
                        $temp['CurrentPassword'] = $this->input->post('txtCurrentPassword');
                        $temp['flag'] = FALSE;    
                    } elseif (strcmp(md5($this->input->post('txtNewPassword')), $data['Password']) == 0) {
                        $temp['message'] = $this->lang->line('Home_TooSimilar');
                        $temp['CurrentPassword'] = $this->input->post('txtCurrentPassword');
                        $temp['flag'] = FALSE;                    
                    } else {
                        $id = $this->session->userdata('UserId');
                        $arrData = array('Password' => md5($this->input->post('txtNewPassword')));
                        if ($this->User->updateDataName($arrData, $id, "UserId")) {
                            $temp['message'] = $this->lang->line('Home_UpdateSucessfull');
                            $temp['flag'] = TRUE;
                        } else {
                            $temp['message'] = $this->lang->line('Home_UpdateError');
                            $temp['flag'] = FALSE;
                        }
                    }
                } else {
                    $temp['message'] = $this->lang->line('Home_CurrentPaswordWrong');
                    $temp['CurrentPassword'] = $this->input->post('txtCurrentPassword');
                    $temp['flag'] = FALSE;
                }
            } else {
                $temp['CurrentPassword'] = $this->input->post('txtCurrentPassword');
            }
        }
        $temp['js'] = array('jquery-ui.js');
        $temp['css'] = array('jquery-ui.css');
        $temp['dialogTitle'] = $this->lang->line('Home_DialogChangePassWord');
        $temp['title'] = $this->lang->line('Home_ChangePassWord');
        $temp['show'] = "changepass";
        return $temp;
    }

    /*
     * @author :        HungLV
     * @created:        
     * @Description:    Send News Email for User
     * @Input :         Null
     * @Output :        Return True Or False
     */

    function resetPassWord() {
        $temp = array();
        $this->form_validation->set_error_delimiters('', '');
        $this->form_validation->set_rules('txtEmail', $this->lang->line('Home_Email'), 'required|valid_email');
        if ($this->input->post('txtEmail')) {
            $temp['txtEmail'] = $this->input->post('txtEmail');
        } else {
            $temp['txtEmail'] = set_value('txtEmail');
        }
        if ($this->form_validation->run() == TRUE) {
            $Email = $this->input->post('txtEmail');
            // check email exit database
            if ($this->checkExitEmail($Email)) {
                $this->email->from('demo.aircon@gmail.com', $this->lang->line('Home_EmailName'));
                $this->email->to($Email);
                $this->email->subject($this->lang->line('Home_EmailReset'));
                // genera new password
                $newpassword = $this->getRandomString();
                // get Info User by Email
                $infoUser = $this->getInfoUserByEmail($Email);
                // content for send email
                $content = $this->lang->line('Home_TitleEmail');
                $content .= "<br>";
                $content .= "<p>";
                $content .= $this->lang->line('Home_FullName');
                $content .= " : " . $infoUser['FullName'];
                $content .= "</p>";
                $content .= "<p>";
                $content .= $this->lang->line('Home_UserName');
                $content .= " : " . $infoUser['UserName'];
                $content .= "</p>";
                $content .= "<p>";
                $content .= $this->lang->line('Home_NewPassword');
                $content .= " : <i><b>" . $newpassword . "</b></i>";
                $content .= "</p>";
                $this->email->message($content);
                if ($this->email->send()) {
                    // update new password
                    $data = array(
                        'Password' => md5($newpassword)
                    );
                    if ($this->updateDataName($data, $Email, 'Email')) {
                        $temp['flag'] = TRUE;
                        $temp['message'] = $this->lang->line('Home_resetsuccess');
                    } else {
                        $temp['flag'] = FALSE;
                        $temp['message'] = $this->lang->line('Home_resetfail');
                    }
                } else {
                    $temp['flag'] = FALSE;
                    $temp['message'] = $this->lang->line('Home_resetfail');
                }
            } else {
                $temp['flag'] = FALSE;
                $temp['message'] = $this->lang->line('Home_EmailNoExit');
            }
        }
        return $temp;
    }

    /*
     * @author :        HungLV
     * @created:        
     * @Description:    set info for reset password
     * @Input :         Null
     * @Output :        Return Array for view
     */

    function getInfoResetPassWord() {
        $data = array(
            'show' => 'resetpass',
            'js' => array('jquery-ui.js'),
            'css' => array('jquery-ui.css'),
            'title' => $this->lang->line('Home_resetpassword'),
            'more1' => $this->lang->line('Home_more1'),
            'more2' => $this->lang->line('Home_more2'),
        );
        return $data;
    }

    /*
     * @author :        HungLV
     * @created:        
     * @Description:    Check Exit Email
     * @Input :         (variable) $mail
     * @Output :        Return true or flase
     */

    function checkExitEmail($mail) {
        if (isset($mail) && $mail) {
            $this->db->from($this->table_name);
            $this->db->where('Email', $mail);
            $query = $this->db->get();
            if ($query->num_rows() > 0) {
                return TRUE;
            } else {
                return FALSE;
            }
        } else {
            return FALSE;
        }
    }

    /*
     * @author :        HungLV
     * @created:        
     * @Description:    Get Info User By Email
     * @Input :         $Email
     * @Output :        Return array
     */

    function getInfoUserByEmail($Email) {
        $this->db->from($this->table_name);
        $this->db->where('Email', $Email);
        $query = $this->db->get();
        return $query->row_array();
    }

    /*
     * @author :        HungLV
     * @created:        
     * @Description:    Random 8 character
     * @Input :         null
     * @Output :        Return 8 character
     */

    function getRandomString($length = 8) {
        $validCharacters = "abcdefghijklmnopqrstuxyvwzABCDEFGHIJKLMNOPQRSTUXYVWZ";
        $validCharNumber = strlen($validCharacters);

        $result = "";

        for ($i = 0; $i < $length; $i++) {
            $index = mt_rand(0, $validCharNumber - 1);
            $result .= $validCharacters[$index];
        }

        return $result;
    }

    /*
     * @author :        Quyet
     * @created:        7/7/2014
     * @Description:    push range year to session
     * @Input :         (Integer) $page
     * @Output :        (Array) data for view data
     */

    function setRangeYear() {
        $rangeYear = $this->config->item('rangeYear');
        if (!(is_array($rangeYear) && count($rangeYear) && $rangeYear['maxYear'] > $rangeYear['minYear'])) {
            $rangeYear = array(
                'minYear' => 1940,
                'maxYear' => 2050,
            );
        }
        $this->session->set_userdata($rangeYear);
    }
    
    /**
     * @author Thanh NV        
     * @created:2014/08/25
     * @function getCompany 
     * @param: $CompanyCode (int)          
     * @Output: array
     * @This function return array data of compay
     */ 
    function getCompany($CompanyCode=1){
        $condition=array('where' =>array('CompanyCode'=>$CompanyCode));
        $arrData=$this->selectDB($this->table_company, $condition);
        return $arrData;
                    
    }

}

?>